package com.pic.mgt.haishin.converter;

import java.io.File;

import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.FormatParser;

public class AppointedTargetHaishinMessageUnitConverter extends
											HaishinMessageUnitConverter {
	private FormatParser appointedTargetMessageTransactionFileParser;
	private FormatParser appointedConfirmationFileForTxFileParser;
	
	
	@Override
	/*
	 *  覆寫 MessageUnitConverter的TransactionFile填入,因為指示配信的目標檔案
	 *  交易訊息(Message),其中的確認檔和交易檔並非相同的檔名。
	 */
	protected void fillTransactionFile() throws WrongUnitFormatException {
		try {
			File transactionFile = (File)
						this.appointedTargetMessageTransactionFileParser.
														parse(super.confirmationFile);
			super.message.setTransactionFile(transactionFile);
		} catch (WrongUnitFormatException e) {
			if (e.getMessage().indexOf("doesn't exist") >= 0 ) {
				// 如果是找不到交易的檔案，則忽略（只有訊息是如此！）
			}
			else {
				throw e;
			}
		}
	}
	
	@Override
	/*
	 *  覆寫 MessageUnitConverter的確認檔(for交易檔的確認檔)填入,因為指示配信的目標檔案
	 *  交易訊息(Message),其中的確認檔和交易檔並非相同的檔名。
	 */
	protected void fillConfirmationFileForTxFile()
			throws WrongUnitFormatException {
		try {
			super.message.setConfirmationFileForTxFile((File)
					this.appointedConfirmationFileForTxFileParser.parse(super.confirmationFile));
		} catch (WrongUnitFormatException e) {
			if (e.getMessage().indexOf("doesn't exist") >= 0 ) {
				// 如果是找不到交易檔的確認檔，則忽略（只有訊息是如此！）
			}
			else {
				throw e;
			}
		}
	}

	// --- getter and setter ---
	
	public void setAppointedTargetMessageTransactionFileParser(
			FormatParser appointedTargetMessageTransactionFileParser) {
		this.appointedTargetMessageTransactionFileParser = appointedTargetMessageTransactionFileParser;
	}

	public void setAppointedConfirmationFileForTxFileParser(
			FormatParser appointedConfirmationFileForTxFileParser) {
		this.appointedConfirmationFileForTxFileParser = appointedConfirmationFileForTxFileParser;
	}
	
}
